﻿using WellDone.ServiceReferenceOpenRPOS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class AddLayout : ChildWindow
    {
        public int _layoutID = 0;

        public AddLayout()
        {
            InitializeComponent();
        }

        private async void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            if (_layoutID >0)
            {
                await Globals.db.GetTableLayoutObjects("ID=" + _layoutID);

                if (Globals.db.TableLayoutRecords.Count > 0)
                {
                    txtLayoutName.Text = Globals.db.TableLayoutRecords[0].col_Name;
                    txtHeight.Text = Convert.ToString(Globals.db.TableLayoutRecords[0].col_Width);
                    txtWidth.Text = Convert.ToString(Globals.db.TableLayoutRecords[0].col_Length);
                }
            }
        }


        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // check validation 
            if (IsValid() == false)
            {
                return;
            }

            await Globals.db.GetTableLayoutObjects("ID <> " + _layoutID + " and Name = '" + txtLayoutName.Text + "'"); //check for duplicate layout 
            if (Globals.db.TableLayoutRecords.Count > 0)
            {
                MessageBox.Show("Table layout already exists. Please enter a different name.");
                txtLayoutName.Focus();
                return;
            }

            await Globals.db.GetTableLayoutObjects("ID=" + _layoutID);
            if (Globals.db.TableLayoutRecords.Count > 0)
            {

                Globals.db.TableLayoutRecords[0].bFilterLength = true;
                Globals.db.TableLayoutRecords[0].bFilterName = true;
                Globals.db.TableLayoutRecords[0].bFilterWidth = true;
                Globals.db.TableLayoutRecords[0].col_Name = txtLayoutName.Text.Trim();
                Globals.db.TableLayoutRecords[0].col_Width = (string.IsNullOrWhiteSpace(txtHeight.Text) ? 0 : Convert.ToDouble(txtHeight.Text));
                Globals.db.TableLayoutRecords[0].col_Length = (string.IsNullOrWhiteSpace(txtWidth.Text) ? 0 : Convert.ToDouble(txtWidth.Text));
                await Globals.db.UpdateTableLayoutObject(Globals.db.TableLayoutRecords[0]);
            }
            else
            {
                TableLayout oLayoutLayout = new TableLayout()
                {
                    bFilterLength = true,
                    bFilterName = true,
                    bFilterWidth = true,
                    col_Name = txtLayoutName.Text.Trim(),
                    col_Width = (string.IsNullOrWhiteSpace(txtHeight.Text) ? 0 : Convert.ToDouble(txtHeight.Text)),
                    col_Length = (string.IsNullOrWhiteSpace(txtWidth.Text) ? 0 : Convert.ToDouble(txtWidth.Text)),
                };

                int iNewReord = -1;
                iNewReord = await Globals.db.AddTableLayoutObject(oLayoutLayout);
                
                //Get New id  
                await Globals.db.GetTableLayoutObjects("Name='" + txtLayoutName.Text.Trim() + "'");
                if (Globals.db.TableLayoutRecords.Count > 0) _layoutID = Globals.db.TableLayoutRecords[0].col_ID;

            }

            this.DialogResult = true;
        }

        /// <summary>
        /// check validation for valid layout 
        /// </summary>
        /// <returns>retrurn true if layout is valid </returns>
        private bool IsValid()
        {
            if (string.IsNullOrWhiteSpace(txtLayoutName.Text.Trim()))
            {
                MessageBox.Show("Layout name should not be empty.");
                txtLayoutName.Focus();
                return false;
            }
            else if (string.IsNullOrWhiteSpace(txtHeight.Text.Trim()))
            {
                MessageBox.Show("Height should not be empty.");
                txtHeight.Focus();
                return false;
            }
            else if (string.IsNullOrWhiteSpace(txtWidth.Text.Trim()))
            {
                MessageBox.Show("Width  should not be empty.");
                txtWidth.Focus();
                return false;
            }


            if (Convert.ToDouble(txtHeight.Text) < 100 || Convert.ToDouble(txtWidth.Text) < 100)
            {
                MessageBox.Show("Warning : Height and width  should be greater than 100.");
            }

            return true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void txtHeight_KeyDown(object sender, KeyEventArgs e)
        {
            DecimalExtensions.SetDecimalTextBox(sender, e);
        }

        private void txtWidth_KeyDown(object sender, KeyEventArgs e)
        {
            DecimalExtensions.SetDecimalTextBox(sender, e);
        }

        private void txtLayoutName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

        



    }
}

